package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.terminalDao.YhbTerminalDao;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.service.TransactionInfoService;
import com.framework.service.yhbservice.YhbCusTerService;
import com.framework.service.yhbservice.YhbTerminalService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.service.yhbservice.YhbUserTerminalRelationService;
import com.framework.utils.DateUtils;
import com.framework.yhbEntity.user.YhbCusTer;
import com.framework.yhbEntity.user.YhbUser;
import com.framework.yhbEntity.user.YhbUserTerminalRelation;
import com.framework.yhbEntity.view.TerminalInfoResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 */
@Service("yhbTerminalService")
public class YhbTerminalServiceImpl extends ServiceImpl<YhbTerminalDao, TerTerminalInfo> implements YhbTerminalService {

    @Autowired
    private YhbUserService yhbUserService;
    @Autowired
    private YhbUserTerminalRelationService yhbUserTerminalRelationService;

    @Autowired
    private YhbCusTerService yhbCusTerService;

    @Autowired
    private TransactionInfoService transactionInfoService;


    @Override
    public Map<String, Object> queryTerminalInfoList(String token) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        Long deptId = user.getDeptId();
        if (deptId.equals(2L)) {
            deptId = 1L;
        }
        List<TerminalInfoResultView> list = baseMapper.queryTerminalInfoListByUserId(user.getUserId(), deptId);
        Map<String, Object> map = new HashMap<>(1);
        for (TerminalInfoResultView view : list) {
            String remark = null;
            Integer state = view.getState();
            String isVip = view.getIsVip();
            if (state.intValue() == 1) {
                if (view.getIsActivity().intValue() == 1) {
                    if ("否".equals(isVip)) {
                        if (view.getTerminalType().intValue() == 10 || view.getTerminalType().intValue() == 11) {
                            BigDecimal jdPayRate = getJDPayRate(view.getSn());
                            if (jdPayRate.compareTo(new BigDecimal("0.00")) != 1) {
                                remark = "注:交易笔数连续达标则享受万3.8的返佣";
                            } else {
                                remark = "注:该机器享受万" + jdPayRate + "的分润";
                            }

                        } else {
                            remark = "注:该机器享受万" + view.getYhbRate() + "的分润";
                        }
                    } else {
                        remark = "注:该机器为VIP机器,不享受分润";
                    }
                } else if (view.getIsActivity() == 0) {
                    remark = "注:该机器未参与活动";
                }
            } else if (state.intValue() == 2) {
                remark = "注:该机器正在审核中";
            } else {
                remark = "注:该机器被拒绝";
            }
            view.setRemark(remark);
        }


        map.put("list", list);
        return map;
    }

    public BigDecimal getJDPayRate(String sn) {
        int count = transactionInfoService.selectCount(new EntityWrapper<TranTransactionInfo>().eq("sn", sn));
        if (count >= 300) {
            Page<TranTransactionInfo> page = transactionInfoService.selectPage(new Page<TranTransactionInfo>(0, 300), new EntityWrapper<TranTransactionInfo>().eq("sn", sn)
                    .orderBy("transaction_date", true)
            );
            List<TranTransactionInfo> records = page.getRecords();
            Date transactionDate = records.get(299).getTransactionDate();

            Date date = DateUtils.addDateMonths(transactionDate, 3);

            Date today = new Date();

            if (date.compareTo(today) == 1) {
                return new BigDecimal("0.00");
            } else {
                return new BigDecimal("3.8");
            }
        } else {
            return new BigDecimal("0.00");
        }
    }


    @Override
    public TerTerminalInfo queryTerminalInfoBySn(String sn) {
        TerTerminalInfo terminalInfo = new TerTerminalInfo();
        terminalInfo.setSn(sn);
        TerTerminalInfo t = baseMapper.selectOne(terminalInfo);
        return t;
    }

    @Override
    public List<Map<String, Object>> getTerminalExample(Long deptId) {
        if (deptId.equals(2L)) {
            deptId = 1L;
        }
        List<Map<String, Object>> list = baseMapper.getTerminalExample(deptId);
        return list;
    }

    @Override
    public void deleteTerminal(String token, String sn) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        YhbUserTerminalRelation r = yhbUserTerminalRelationService.queryRelationBySn(sn);
        if (r == null) {
            throw new RRException("9000", "此设备未绑定");
        }
        if (!r.getUserId().equals(user.getUserId())) {
            throw new RRException("9000", "此终端不属于你，无法删除");
        }
        r.setIsDel(1);
        yhbUserTerminalRelationService.deleteTerminal(r);


        TerTerminalInfo t = queryTerminalInfoBySn(sn);
        if (t == null) {
            throw new RRException("9000", "未查询到该终端");
        }

        t.setIsYhbuse(0);
        baseMapper.updateById(t);
        if (t.getIsTradition().intValue() == 1) {
            YhbCusTer yct = yhbCusTerService.queryOneBySn(sn);
            if (yct != null) {
                yct.setIsDel(1);
                yhbCusTerService.updateById(yct);
            }
        }

    }

    @Override
    public void setIsYhbUse(String sn) {
        TerTerminalInfo t = queryTerminalInfoBySn(sn);
        t.setIsYhbuse(1);
        baseMapper.updateById(t);
    }

    @Override
    public PageUtils queryTerminalPage(Map<String, Object> params) {
        Integer currpage = Integer.valueOf(params.get("page").toString());
        Integer pageSize = Integer.valueOf(params.get("limit").toString());
        params.put("start", (currpage - 1) * pageSize);
        params.put("limit", pageSize);
        List<Map<String, Object>> list = baseMapper.queryTerminalList(params);
        Integer count = baseMapper.queryTerminalCount(params);
        return new PageUtils(list, count, pageSize, currpage);
    }

    @Override
    public void distribuTer(Map<String, Object> q) {

    }

    @Override
    public List<String> getActivityTerminal(String userId, String terminalType) {
        List<String> list = baseMapper.getActivityTerminal(userId, terminalType);
        return list;
    }


}
